/*
 * @Author: ZhangZheyi
 * @Date:   2017-01-17 16:45:26
 * @Last Modified by:   ZhangZheyi
 * @Last Modified time: 2017-01-17 17:53:18
 */

module.exports = {
    /*
      接口公共域名地址配置
    */

    api: '//erp.banksteel.com/api',

    /**
     * 获取cookie
     * @param {string} name
     */
    getCookie: function (name) {
        var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
        if (arr != null) {
            return unescape(arr[2]);
        }
        return null;
    },

    validate: function(vue, fn, validationName){
         validationName = validationName || 'validation';
         var v = vue['$' + validationName];
         setTimeout(function(){
             if(v.invalid){
                 var error = v.errors[v.errors.length-1];
     
                 module.exports.alertPop(vue, error.message, function(){
                     var names='+error.field+'
                     document.getElementsByName(names).onfocus
                 })
             }else{
                 fn.apply(vue);
             }
         },500)
     },

     alertPop:function (vue ,msg, callback) {
        vue.showAlert =true;
        vue.optAlert ={
            button:"确定",
            content:msg,
            onOk: callback || function(){}
        }
     },

     /*
     生成唯一id jsonp 回调函数
      */
      guid:function () {
        return 'xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {  
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);  
        return v.toString(16);  
        });  
      },


    /**
     * @description 判断变量类型
     * @memberof util
     * @param value {*} 需要判断的值
     * @returns {string} 返回具体类型如：[object String],[object Array]等
     */
    typeof: function (value) {
        return Object.prototype.toString.call(value);
    },
    
    /**
     * @namespace
     * @memberof EM
     * @description 本地存储操作
     * @type {object}
     */
    ls: {
        /**
         * 设置key
         * @param {string} key
         * @param {string|array|object} value
         */
        set: function (key, value) {
            if (this.get(key) !== null) {
                this.remove(key);
            }
            if (!value) {
                value = null;
            }
            if (typeof(value) === '[object Object]' || typeof(value) === '[object Array]') {
                value = JSON.stringify(value);
            }
            localStorage.setItem(key, value);
        },

        /**
         * 获取key
         * @param {string} key
         */
        get: function (key) {
            var v = localStorage.getItem(key);
            v = v === undefined ? null : v;
            if (v === null) {
                return v;
            }
            try {
                return JSON.parse(v);
            } catch (e) {
                return v;
            }
        },
        /**
         * 移除指定key
         * @param {string} key
         */
        remove: function (key) {
            localStorage.removeItem(key);
        },
        /**
         * 移除全部key
         */
        clear: function () {
            localStorage.clear();
        },
        /**
         * 遍历所有存在的key
         * @param {function} fn 遍历的回调 function(key, value){}
         */
        each: function (fn) {
            var n = localStorage.length,
                i = 0,
                _fn = fn || function () {},
                key;
            for (; i < n; i++) {
                key = localStorage.key(i);
                if (_fn.call(this, key, this.get(key)) === false) {
                    break;
                }
                if (localStorage.length < n) {
                    n--;
                    i--;
                }
            }
        }
    }

};